/**
 * @Description: axios的二次封装
 * @MethodAuthor: 谢超
 * @Date: 2023-07-07 16:50:05
 */
import axios, { type AxiosRequestConfig } from 'axios';

const service = axios.create({
  baseURL: import.meta.env.VITE_BASE_URL,
  timeout: 10000,
});

// 添加请求拦截器
service.interceptors.request.use((config) => {
  const { headers } = config;

  return config; // 最后必须返回这个请求配置,不然调不了接口
});

// 添加响应拦截器
service.interceptors.response.use(
  (response) => {
    const { config, headers, data } = response;
    console.log('success');
    return data;
  },
  (error) => {
    const status = error.response.status;
    switch (status) {
      case 401:
        break;
      case 403:
        break;
      case 404:
        break;
      case 500:
        break;
      default:
    }

    return Promise.reject(error);
  }
);

export const $get = (url: string, config: AxiosRequestConfig) => {
  console.log(service.get(url, config));

  return service.get(url, config);
};

/**
 * @Description: axios对请求方式的封装
 * @MethodAuthor: 谢超
 * @Date: 2023-07-31 14:33:36
 * @param {string} url 请求url
 * @param {object} options 请求方式
 * @param {object} args 具体参数
 * @param {object} tip 自定义的提示语
 * @return {Promise}
 */
const request = (url: string, options: any, args: any) => {
  console.log(url, options, args);

  return service[options](url, args);
};

export default request;
